--[[
   file:download.lua
   author:yuan jie
   date:2015-06-17
   description:This lua file is service as download business
   vesion: v1.0
   modified history:
--]]

local cjson = require "cjson";
local parser = require "redis.parser";

local res = ngx.location.capture("/redis_get",{args = { key = ngx.var.arg_id }});

if res.status == 200 then

	local reply = parser.parse_reply(res.body);
	if reply == nil then return 404 end
	
	local url_json = cjson.decode(reply);
	if url_json == nil then return 404 end

	--[[ format download url --]]
	local file_guid = url_json["guid"] or "no_file_guid";
	local file_name = url_json["name"] or "no_file_name";
    local host_name = ngx.var.host or "www.unclone.cn";
	local url = "http://"..host_name.."/"..file_guid.."?file_source_name="..file_name;
	--[[ redirect --]]
	return ngx.redirect(url);
else
	return 404;
end
